package com.qdsg.ysg.doctor.nurse.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qdsg.ysg.doctor.R;
import com.qdsg.ysg.doctor.base.BaseActivity;
import com.qdsg.ysg.doctor.ui.WebViewActivity;
import com.qdsg.ysg.doctor.ui.adapter.NurseInfoAdapter;
import com.qdsg.ysg.doctor.ui.adapter.NurseItemsAdapter;
import com.qdsg.ysg.doctor.util.ToastUtil;
import com.qdsg.ysg.doctor.util.UtilString;
import com.rest.business.RestProxy;
import com.rest.response.BaseResponse;
import com.rest.response.PatientProjectDTO;
import com.rest.response.PrescriptionResponse;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.core.widget.NestedScrollView;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 待接单
 * Created by lbj on 2022/3/28 9:51
 */
public class AtyWaitAccept extends BaseActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_look_record)
    TextView tvLookRecord;
    @BindView(R.id.tv_person_name)
    TextView tvPersonName;
    @BindView(R.id.tv_person_sex)
    TextView tvPersonSex;
    @BindView(R.id.tv_person_age)
    TextView tvPersonAge;
    @BindView(R.id.tv_person_tel)
    TextView tvPersonTel;
    @BindView(R.id.tv_person_address)
    TextView tvPersonAddress;
    @BindView(R.id.tv_person_time)
    TextView tvPersonTime;
    @BindView(R.id.cl_address)
    ConstraintLayout clAddress;
    @BindView(R.id.rv_info)
    RecyclerView rvInfo;
    @BindView(R.id.rv_items)
    RecyclerView rvItems;
    @BindView(R.id.nested_scrollView)
    NestedScrollView nestedScrollView;
    @BindView(R.id.tv_accept)
    TextView tvAccept;
    @BindView(R.id.tv_reject)
    TextView tvReject;
    @BindView(R.id.ll_bottom)
    LinearLayout llBottom;
    List<String> mInfoList = new ArrayList<>();
    List<String> mItemList = new ArrayList<>();
    List<PrescriptionResponse.Second> list2 = new ArrayList<>();

    String appointmentId, docAppointmentId, patientId;
    Boolean isGuoYao = false, isCanNext = false, isCan;

    private NurseInfoAdapter nurseInfoAdapter;
    private NurseItemsAdapter nurseItemsAdapter;


    @Override
    protected int getLayoutResource() {
        return R.layout.aty_wait_accept;
    }

    @Override
    protected void initData() {
        appointmentId = getIntent().getStringExtra( "appointmentId" );
        docAppointmentId = getIntent().getStringExtra( "docAppointmentId" );
        if (!UtilString.isEmpty( appointmentId )) {
            appointmentDetail();
        }
    }


    @Override
    protected void initViews() {
        tvTitle.setText( "订单详情" );
        LinearLayoutManager managerInfo = new LinearLayoutManager( mContext );
        managerInfo.setOrientation( LinearLayoutManager.VERTICAL );
        rvInfo.setLayoutManager( managerInfo );
        LinearLayoutManager manager = new LinearLayoutManager( mContext );
        manager.setOrientation( LinearLayoutManager.VERTICAL );
        rvItems.setLayoutManager( manager );
        nurseInfoAdapter = new NurseInfoAdapter( mContext, mInfoList );
        rvInfo.setAdapter( nurseInfoAdapter );
        nurseItemsAdapter = new NurseItemsAdapter( mContext, mItemList );
        rvItems.setAdapter( nurseItemsAdapter );

    }

    @OnClick({R.id.tv_look_record, R.id.tv_accept, R.id.tv_reject})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_look_record:
                Bundle bundleHistory = new Bundle();
                bundleHistory.putString( "patientId", patientId );
                bundleHistory.putString( "fromWhere", "CloudListActivity" );
                startActivity( WebViewActivity.class, bundleHistory );
                break;
            case R.id.tv_accept:
                updateForOrder( "2" );
                break;
            case R.id.tv_reject:
                updateForOrder( "3" );
                break;
        }
    }

    private void updateForOrder(String type) {
        showProgressDialog( mContext );
        Map<String, Object> map = new HashMap<>();
        map.put( "docAppointmentId", appointmentId );
        map.put( "type", type );
        RestProxy.getInstance().updateForOrder( map, new Observer<BaseResponse>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseResponse baseResponse) {
                if ("2".equals( type )) {
                    ToastUtil.showToastCenter( mContext, "接单成功" );
                    llBottom.setVisibility( View.GONE );
                }else {
                    finish();
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
                dismissProgressDialog();
            }

            @Override
            public void onComplete() {
                dismissProgressDialog();
            }
        } );


    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult( requestCode, resultCode, data );
        if (resultCode != RESULT_OK) {
            return;
        }
        if (requestCode == 10000) {
            setResult( RESULT_OK );
            finish();
        }
    }


    private void appointmentDetail() {
        RestProxy.getInstance().appointmentDetail( appointmentId, new Observer<PatientProjectDTO>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @SuppressLint("SetTextI18n")
            @Override
            public void onNext(PatientProjectDTO patientProjectDTO) {
                try {
                    patientId = patientProjectDTO.data.patientId;
                    tvPersonName.setText( "姓名：" + patientProjectDTO.data.patientName );
                    tvPersonSex.setText( "性别：" + patientProjectDTO.data.sex );
                    tvPersonAge.setText( "年龄：" + patientProjectDTO.data.age + "岁" );
                    tvPersonAddress.setText( "地址：" + patientProjectDTO.data.adress );
                    tvPersonTime.setText( "预约时间：" + patientProjectDTO.data.appointmentTime );
                    tvPersonTime.setText( "联系方式：" + patientProjectDTO.data.patientPhone );
                    mInfoList.clear();
                    if ("1".equals( patientProjectDTO.data.isEyeRed )) {
                        mInfoList.add( "眼睛是否红、肿、痛：是" );
                    } else {
                        mInfoList.add( "眼睛是否红、肿、痛：否" );
                    }
                    if ("1".equals( patientProjectDTO.data.isSecretion )) {
                        mInfoList.add( "有无分泌物：有分泌物" );
                    } else {
                        mInfoList.add( "有无分泌物：无分泌物" );
                    }
                    if ("1".equals( patientProjectDTO.data.isTrichiasis )) {
                        mInfoList.add( "睫毛有无倒睫：有倒睫" );
                    } else {
                        mInfoList.add( "睫毛有无倒睫：无倒睫" );
                    }
                    if ("1".equals( patientProjectDTO.data.isUnobstructed )) {
                        mInfoList.add( "泪道是否通畅：通畅" );
                    } else {
                        mInfoList.add( "泪道是否通畅：不通畅" );
                    }
                    if ("1".equals( patientProjectDTO.data.isSuture )) {
                        mInfoList.add( "眼部是否有缝线：有缝线" );
                        mInfoList.add( "缝线位置：" + patientProjectDTO.data.suturePosition );
                        if ("1".equals( patientProjectDTO.data.isRemove )) {
                            mInfoList.add( "是否需要拆线：需要" );
                        } else {
                            mInfoList.add( "是否需要拆线：不需要" );
                        }

                    } else {
                        mInfoList.add( "眼部是否有缝线：无缝线" );
                    }
                    nurseInfoAdapter.notifyDataSetChanged();

                    mItemList.clear();
                    mItemList.add( patientProjectDTO.data.projectTitle );
                    nurseItemsAdapter.notifyDataSetChanged();

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable e) {
                ToastUtil.onError( mContext, e );
            }

            @Override
            public void onComplete() {

            }
        } );

    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        // TODO: add setContentView(...) invocation
        ButterKnife.bind( this );
    }
}
